<template>
  <a-card class="content-view" :bordered="false">
    <div v-for="(item,index) in configList" :key="index" class="config-item">
      <div class="config-item-label">{{ configKeys[item.domain] }}</div>
      <a-input
        v-if="item.domain == 'mini'"
        class="config-item-content"
        v-model="item.configval"
        @input="e => item.configval = checkNum(e.target.value)"
        :disabled="!$auth('SYSTEM.SYSTEM_SETTING_EDIT')"
      ></a-input>
      <a-radio-group v-else-if="item.domain == 'eid'" v-model="item.configval" >
        <a-radio :value="'0'">{{ $t('all.autotext1138') }}</a-radio>
        <a-radio :value="'1'">{{ $t('all.autotext1239') }}</a-radio>
      </a-radio-group>
      <a-input-number
        v-else
        v-model="item.configval"
        class="config-item-content"
        :min="1"
        :max="9999"
        :parser="value => value.replace(/[^\d]/g, '')"
        :disabled="!$auth('SYSTEM.SYSTEM_SETTING_EDIT')"
      >
      </a-input-number>
    </div>
    <div class="config-item" style="margin-top:20px">
      <div class="config-item-label">{{ $t('all.autotext173') }}</div>
      <a-button type="primary" @click="reAuth">{{ $t('all.autotext697') }}</a-button>
    </div>
    <div class="config-item" style="margin-top:20px">
      <div class="config-item-label"></div>
      <a-button v-if="$auth('SYSTEM.SYSTEM_SETTING_EDIT')" type="primary" @click="configUpdate">{{ $t('all.autotext321') }}</a-button>
    </div>
  </a-card>
</template>
<script>
import { getConfig, configUpdate, selectOperation, updateReauthorization } from '@/api/system/setting'

export default {
  data () {
    return {
      miniKey: 'mini',
      miniData: null,
      configKeys: {
        'mini': this.$t('all.autotext1223'),
        'pwd': this.$t('all.autotext1203'),
        'eid': this.$t('all.autotext1115')
      },
      configList: []
    }
  },
  mounted () {
    this.getConfig()
  },
  methods: {
    async getAgreeId () {
      const res = await selectOperation({
        firstKey: 'agreement',
        secondKey: 'epayment'
      })
      return res.id
    },
    async reAuth () {
      const agreeId = await this.getAgreeId()
      await updateReauthorization({
        id: agreeId,
        content: 1
      })
      this.$message.success(this.$t('all.autotext470'))
    },
    getConfig () {
      if (this.$auth('SYSTEM.SYSTEM_SETTING_LIST')) {
        getConfig().then(res => {
          this.configList = res
        })
      }
    },
    configUpdate () {
      configUpdate({ json: JSON.stringify(this.configList) }).then(res => {
        this.$notification.success({
          message: this.$t('all.autotext398')
        })
      })
    },
    checkNum (val) {
      val = val.replace('e', '')
      val = val.replace(/[^0-9.]/g, '')
      val = val.replace(/^()*(\d+)\.(\d\d).*$/, '$1$2.$3')
      if (val >= 100) {
        val = 100
      }
      if (val < 0) {
        val = 0
      }
      return val
    }
  }
}
</script>
<style lang="less" scoped>
  .content-view{
    padding: 10px 0;
  }
  .config-item{
    display: flex;
    margin-bottom: 10px;
    align-items: center;
    &-label{
      width: 220px;
      text-align: right;
      padding-right: 15px;
    }
    &-content{
      width: 300px;
    }
  }
</style>
